//package com.ruoyi.demo.task;
//
//import com.alibaba.fastjson2.JSONObject;
//import com.ruoyi.demo.service.*;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.ServletContext;
//
//@Component
//public class JYTokenDemoScheduler implements InitializingBean {
//    @Autowired
//    ServletContext servletContext;
//    @Autowired
//    JyDemoService jyService;
//    /**
//     * 项目启动时获取到访问极云的Token，将其存储在servletContext中
//     */
//    @Override
//    public void afterPropertiesSet()  {
//        String jsonStr = jyService.getAccessToken();
//        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
//        //将 access_token 和 refresh_token 设置到容器中
//        String accessToken = jsonObject.getString("access_token");
//        String refresh_token = jsonObject.getString("refresh_token");
//        jsonObject.getString("access_token");
//        if (accessToken != null) {
//            servletContext.setAttribute("access_token", accessToken);
//            servletContext.setAttribute("refresh_token",refresh_token);
//            System.out.println("访问极云的Token是" + accessToken);
//            System.out.println("访问极云的refreshToken是" + refresh_token);
//        }
//    }
//
//    /**
//     * 每一小时刷新一次token
//     */
//    @Scheduled(cron = "0 0 */1 * * ?")
//    public void refreshTokenEveryNinetyMinutes() {
//        String jsonStr = jyService.getNewAccessToken();
//        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
//        String access_token = jsonObject.getString("access_token");
//        String refresh_token = jsonObject.getString("refresh_token");
//
//        if (access_token != null) {
//            servletContext.setAttribute("access_token", access_token);
//            servletContext.setAttribute("refresh_token",refresh_token);
//            System.out.println("新的极云access_token是:" + (String) servletContext.getAttribute("access_token"));
//            System.out.println("新的极云access_token是:" + (String) servletContext.getAttribute("refresh_token"));
//        }
//    }
//}
